//
//  BasePubView.m
//  yunqiphone
//
//  Created by Maple on 13-7-4.
//  Copyright (c) 2013年 ci123.com. All rights reserved.
//

#import "BasePubView.h"
#import "MBProgressHUD.h"

#define TITLE_HEIGHT    50
@implementation BasePubView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      _titlePlaceHolder = @"标题";
      _titleText = @"";
      _contentPlaceHolder = @"请输入内容或者插入图片";
      _changeGap = APP_SCREEN_HEIGHT-216-44-TITLE_HEIGHT-20+Status_Gap;
      [self _initView];
    }
    return self;
}

- (void)_initView {
  _title = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, APP_SCREEN_WIDTH, TITLE_HEIGHT)];
  UIView * myView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 13, TITLE_HEIGHT)];
  _title.leftView = myView;
  _title.leftViewMode = UITextFieldViewModeAlways;
  _title.backgroundColor = [UIColor whiteColor];
  _title.background = [UIImage imageNamed:@"tborder"];
  _title.textColor = [UIColor blackColor];
  _title.font = [UIFont systemFontOfSize:16];
  _title.delegate = self;
  _title.placeholder = _titlePlaceHolder;
  _title.text = _titleText;
  _title.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  _title.clearButtonMode = UITextFieldViewModeWhileEditing;
  [self addSubview:_title];
  
  _content = [[UITextView alloc] initWithFrame:CGRectMake(5, _title.bottom, APP_SCREEN_WIDTH, _changeGap)];
  _content.backgroundColor = [UIColor clearColor];
  _content.delegate = self;
  _content.text = _contentPlaceHolder;
  _content.textColor = RGBCOLOR(180, 180, 180);
  _content.font = [UIFont systemFontOfSize:16];
  [self addSubview:_content];
}

- (void)setChangeGap:(NSInteger)changeGap {
  _content.height = changeGap;
}

- (void)setTitleText:(NSString *)titleText {
  _title.text = titleText;
}

- (void)setContentPlaceHolder:(NSString *)contentPlaceHolder{
  _content.text = contentPlaceHolder;
  _contentPlaceHolder = contentPlaceHolder;
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
  if([textView.text isEqualToString:_contentPlaceHolder]) {
    _content.textColor = [UIColor blackColor];
    _content.text = @"";
  }
  return YES;
}

- (NSString *)getTitles {
  return _title.text;
}

- (NSString *)getContents {
  return _content.text;
}

- (void)titleBecomeResign {
  [_title becomeFirstResponder];
}

- (void)hideKeyboard {
  if([_content isFirstResponder]) {
    [_content resignFirstResponder];
  }else{
    [_title resignFirstResponder];
  }
}

- (void)showKeyboardWithContentView {
  [_content becomeFirstResponder];
}

- (BOOL)checkAllContent {
    if (_title.text.length < 1) {
        [self ShowExclaHud:@"标题不得为空"];
        [_title becomeFirstResponder];
        return NO;
    }
  if(_content.text.length < 1 || [_content.text isEqualToString:_contentPlaceHolder]) {
    [self ShowExclaHud:@"内容不得为空"];
      [_content becomeFirstResponder];
    return NO;
  }
  if(_title.text.length < 4) {
    [self ShowExclaHud:@"标题不得小于4个字"];
    return NO;
  }
  if(_title.text.length > 42) {
    [self ShowExclaHud:@"标题不得大于42个字"];
    return NO;
  }
  return YES;
}


- (void)ShowExclaHud:(NSString *)title {
  MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
  hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"excla"]];
  hud.mode = MBProgressHUDModeCustomView;
  if(title.length > 0) {
    hud.labelText = title;
  }
  [hud hide:YES afterDelay:1];
}

- (void)changeContentHeight:(int)height {
   _content.frame = CGRectMake(5, _title.bottom, APP_SCREEN_WIDTH, height);
}

@end
